package com.community.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.communit.dto.ArticleCommentDto;
import com.community.entity.Article;
import com.community.entity.ArticleCommet;
import com.community.entity.User;
import com.community.service.IArticleCommetService;
import com.community.service.IArticleService;
import com.community.service.IUserService;

@Controller
@RequestMapping("/article")
public class ArticleController {
	
	@Autowired
	private IArticleService iArticleService;
	
	@Autowired
	private IArticleCommetService iArticleCommetService;
	
	@Autowired
	private IUserService iuserService;
	
	@RequestMapping("/detail/{id}")
	public String articleDetail(@PathVariable Long id,ModelMap modelMap){
		//获取文章详情
		Article a = iArticleService.getArticleById(id);
		//文章被点击，点击量加1
		a.setClickNum(a.getClickNum()+1);
		iArticleService.updateArticle(a);
		//获取文章评论
		List<ArticleCommet> ArticleCommetList = iArticleCommetService.getArticleCommetListByArticleId(id);
		List<ArticleCommentDto> articleCommentDtoList = new ArrayList<>();
		ArticleCommentDto articleCommentDto;
		for (ArticleCommet articleCommet : ArticleCommetList) {
			articleCommentDto = new ArticleCommentDto();
			User u =iuserService.getUser(articleCommet.getUserid());
			BeanUtils.copyProperties(articleCommet, articleCommentDto);
			articleCommentDto.setNickName(u.getNickName());
			articleCommentDto.setHeadPath(u.getHeadPath());
			articleCommentDtoList.add(articleCommentDto);
			System.out.println(articleCommentDto.getId());
		}
		modelMap.addAttribute("a", a);
		modelMap.addAttribute("articleCommentDtoList", articleCommentDtoList);
		return "/article/article";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public int addArticle(String commentContent,Long articleId,HttpSession session){
		if(session.getAttribute("userid") == null){
			return 0;
		}else{
			ArticleCommet am = new ArticleCommet();
			am.setArticleid(articleId);
			am.setCommentContent(commentContent);
			am.setUserid(Long.valueOf(session.getAttribute("userid").toString()));
			iArticleCommetService.addArticleCommet(am);
			return 1;
		}	
	}
	
	@RequestMapping("/list")
	public String articleList(ModelMap modelMap){
		
		List<Article> articleList = iArticleService.getArticleByLimit(15);
		modelMap.addAttribute("articleList", articleList);
		
		
		List<Article> otherarticleList = iArticleService.getOtherArticleToClickNumByLimit(15);
		modelMap.addAttribute("otherarticleList", otherarticleList);
		
		
		return "/article/article_list";
		
	}

}
